﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DAL;
using System.Data;
using Model;
using System.IO;

namespace BLL
{    
    /// <summary>
    /// 业务逻辑操作类
    /// </summary>
    /// 

        ////
    public class BusinessLogic
    {
        private UserInfoDAL user;  //定义用户数据访问类对象user
        private BookInfoDAL book;
        public BusinessLogic()
        {
            user = new UserInfoDAL();//实例化UserInfo用户数据访问类
            book = new BookInfoDAL();
        }
        // 用户登录业务逻辑实现
        public bool UserLogin(string userid, string password)
        {
            return user.UserLogin(userid, password);
        }
        #region 图书管理模块
        public DataSet GetBookClass()
        {
            return book.GetBookClass();
        }
        /// <summary>
        /// 查重处理
        /// </summary>
        public bool CheckingDulication(BookInfo bo)
        {
            return book.CheckingDulication(bo);
        }
        public BookClass FindBookClass(string bookClassID=null,string bookClassName = null)
        {
            return book.FindBookClass(bookClassID, bookClassName);
        }
        /// <summary>
        /// 图书信息修改
        /// </summary>
        public object EditBook(BookInfo bo,string operation)
        {
            if (operation == "I" && CheckingDulication(bo)==true)
                return "这是重复记录";
            else
            {
                book.EditBook(bo, operation);
                return book;
            }
        }
        /// <summary>
        /// 判断数据集是否为空
        /// </summary>
        /// <param name="ds">数据集</param>
        /// <returns>布尔值</returns>
        public bool Judge(DataSet ds)
        {
            if (ds.Tables.Count == 1 && ds.Tables[0].Rows.Count == 0)
                return false;
            else
                return true;
        }
        /// <summary>
        /// 根据书名、图书类别查询图书信息
        /// </summary>
        /// <param name="bookname">书名</param>
        /// <param name="classname">类别名称</param>
        /// <returns>图书信息数据集</returns>
        public DataSet GetBookByBookNameOrClassname(string bookname,string classname)
        {
            DataSet ds = book.GetBookByBookNameOrClassname(bookname, classname);
            if (Judge(ds))
                return book.GetBookByBookNameOrClassname(bookname, classname);
            else
                return null;
        }
        public DataSet GetBookByBookID(string bookid)
        {
            return book.GetBookByBookID(bookid);
        }
        public bool DeleteBook(string bookid)
        {
            return book.DeleteBook(bookid);
        }
        #endregion
        public DataSet GetUserInfo(string userid=null,string userName = null)
        {
            return user.GetUserInfo(userid, userName);
        }
        public MemoryStream GetPhotoByUserID(string userid)
        {
            return user.GetPhotoByUserID(userid);
        }
        public DataSet GetBorrowBookByUserID(string userid)
        {
            return book.GetBorrowBookByUserID(userid);
        }
        /// <summary>
        /// 新增借书信息
        /// </summary>
        /// <param name="bookid">索书号</param>
        /// <param name="userid">读者证号</param>
        /// <returns>对象</returns>
        public object InsertBorrowInfo(string bookid, string userid)
        {
            if (book.BookSateByBookID(bookid))
                return book.InsertBorrowInfo(bookid, userid);
            else
                return "不可借";
        }
        public DataSet GetBorrowInfo(string userid,string bookid)
        {
             return book.GetBorrowInfo(userid, bookid);
        }
        public bool ReturnBook(string bookid)
        {
            return book.ReturnBook(bookid);
        }
    }
}
